package com.enzo.prms.common.taglib.pagination;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class CollectionTag extends TagSupport {
	private static final long serialVersionUID = 6961772685644570960L;
	private String name,matchCount;

	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		String jsScript = prepareJsScript();
		try {
			System.out.println("matchCount:"+matchCount);
			out.print(jsScript);
		} catch (IOException e) {
			throw new JspException((new StringBuilder()).append(
					"IOException encountered while writing to JspWriter: ")
					.append(e.getMessage()).toString(), e);
		}
		return 6;
	}

	private String prepareJsScript() {
		StringBuffer sb = new StringBuffer();
		sb.append("<script type=\"text/javascript\" language=\"Javascript1.1\"> \n");
		//doShowAll
		sb.append("function doShowAll(){ \n");
		sb.append("var form=document.forms[0];\n");
		sb.append("form.elements['pageInfo.currentpage'].value=\"1\";\n");
		sb.append("form.elements['pageInfo.orderBy'].value=\"\";\n");
		sb.append("form.elements['pageInfo.orderDirection'].value=\"\";\n");
		sb.append("if(form.elements['pageInfo.showAll'].checked){\n");
		sb.append("form.elements['pageInfo.showAll'].value=\"true\";\n");
		sb.append("}else{\n");
		sb.append("form.elements['pageInfo.showAll'].value=\"false\";\n");
		sb.append("}\n");
		sb.append("form.submit();\n");
		sb.append("}\n");
		//topage
		//sb.append("function topage(currentpage,formId){\n");
		//sb.append("var thisForm=document.getElementById(formId);\n");
		sb.append("</script>\n");
		return sb.toString();
	}

	public static boolean getPageContextAttributeAsBoolean(
			PageContext pageContext, String sParameter, boolean iDefaultValue) {
		try {
			Boolean oBln = (Boolean) pageContext.getAttribute(sParameter);
			if (oBln != null)
				return oBln.booleanValue();
		} catch (Exception e) {
			return iDefaultValue;
		}
		return iDefaultValue;
	}

	public static boolean getRequestAttributeAsBoolean(PageContext pageContext,
			String sParameter, boolean iDefaultValue) {
		try {
			Boolean oBln = (Boolean) pageContext.getRequest().getAttribute(
					sParameter);
			if (oBln != null)
				return oBln.booleanValue();
		} catch (Exception e) {
			return iDefaultValue;
		}
		return iDefaultValue;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMatchCount() {
		return matchCount;
	}

	public void setMatchCount(String matchCount) {
		this.matchCount = matchCount;
	}
}
